{capture name="displayTitle"}[[Add New Location]]{/capture}
{capture name="closeButtonText"}[[Close]]{/capture}

{breadcrumbs}[[ZipCode Database]] {/breadcrumbs}
<h1><img src="{image}/icons/rss32.png" border="0" alt="" class="titleicon" />[[ZipCode Database]] </h1>
<p>
	<a href="?action=clear_data" onclick="return confirm('[[Are you sure you want to clear all geographical data?]]')" class="editbutton">[[Clear data]]</a>
	<a href="{$GLOBALS.site_url}/geographic-data/import-data/" class="editbutton">[[Import data from file]]</a>
	<a href="{$GLOBALS.site_url}/geographic-data/add/" class="editbutton"
	   onclick="popUpWindow('{$GLOBALS.site_url}/geographic-data/add/', 335, 430, '{$smarty.capture.displayTitle}'); return false;" >[[Add New Location]]</a>
</p>
<div class="setting_button" id="mediumButton"><strong>[[Click to modify search criteria]]</strong><div class="setting_icon"><div id="accordeonClosed"></div></div></div>
<div class="setting_block" id="clearTable">
	<form method="get" name="search_form" >
		<table  width="100%">
			<tr>
				<td>[[Zip Code]]</td>
				<td><input type="text" name="search[name]" value="{$search.name}"></td>
			</tr>
			<tr>
				<td>[[Longitude]]</td>
				<td><input type="text" name="search[longitude]" value="{$search.longitude}"></td>
			</tr>
			<tr>
				<td>[[Latitude]]</td>
				<td><input type="text" name="search[latitude]" value="{$search.latitude}"></td>
			</tr>
			<tr>
				<td>[[Country]]</td>
				<td>
					<select id="country" name="search[country_sid]" onchange="getStates(this.value);">
						<option value="">[[Any Country]]</option>
						{foreach from=$countries item=country}
							<option value="{$country.id}" {if $search.country_sid == $country.id} selected = "selected"{/if} >{$country.caption}</option>
						{/foreach}
					</select>
				</td>
			</tr>
			<tr>
				<td>[[State]]</td>
				<td>
					<select id="state" name="search[state]" disabled="true" data-old-value="{$search.state}">
						<option value=""></option>
					</select>
				</td>
			</tr>
			<tr>
				<td>[[State Code]]</td>
				<td><input type="text" name="search[state_code]" value="{$search.state_code}"></td>
			</tr>
			<tr>
				<td>[[City]]</td>
				<td><input type="text" name="search[city]" value="{$search.city}"></td>
			</tr>
			<tr>
				<td colspan="2">
					<div class="floatRight">
						<input type="hidden" name="action" value="search"/>
						<input type="submit" value="[[Search]]" class="greenButton"/>
					</div>
				</td>
		</table>
	</form>
</div>
<div class="clr"><br/></div>
<div class="numberPerPage" style="float: left; margin: 15px 15px 0 0;">
	[[per page]]:
	<select id="zip_codes_per_page" name="zip_codes_per_page" onchange="window.location = '?&zip_codes_per_page='+this.value+'&{$query}';" class="perPage">
		<option value="10" {if $zip_codes_per_page == 10}selected="selected"{/if}>10</option>
		<option value="20" {if $zip_codes_per_page == 20}selected="selected"{/if}>20</option>
		<option value="50" {if $zip_codes_per_page == 50}selected="selected"{/if}>50</option>
		<option value="100" {if $zip_codes_per_page == 100}selected="selected"{/if}>100</option>
	</select>
</div>
<div class="pagination" style="float: left;">
{if $page_count > 1}
	<p>
		{if $page_number-1 > 0}<a href="?page_number={$page_number-1}&amp;sorting_field={$sorting_field}&amp;zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">[[Previous]]</a>{/if}
		{if $page_number-3 > 0}<a href="?page_number=1&amp;sorting_field={$sorting_field}&amp;zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">1</a>{/if}
		{if $page_number-3 > 1}...{/if}
		{if $page_number-2 > 0}<a href="?page_number={$page_number-2}&amp;sorting_field={$sorting_field}&amp;zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">{$page_number-2}</a>{/if}
		{if $page_number-1 > 0}<a href="?page_number={$page_number-1}&amp;sorting_field={$sorting_field}&amp;zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">{$page_number-1}</a>{/if}
		<strong>{$page_number}</strong>
		{if $page_number+1 <= $page_count}<a href="?page_number={$page_number+1}&amp;sorting_field={$sorting_field}&amp;zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">{$page_number+1}</a>{/if}
		{if $page_number+2 <= $page_count}<a href="?page_number={$page_number+2}&amp;sorting_field={$sorting_field}&amp;zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">{$page_number+2}</a>{/if}
		{if $page_number+3 < $page_count}...{/if}
		{if $page_number+3 < $page_count + 1}<a href="?page_number={$page_count}&amp;sorting_field={$sorting_field}&amp;zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">{$page_count}</a>{/if}
		{if $page_number+1 <= $page_count}<a href="?page_number={$page_number+1}&amp;sorting_field={$sorting_field}&amp;zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">[[Next]]</a>{/if}
	</p>
{/if}
</div>

<div class="clr"><br/></div>
<form method="post" name="locations_form">
	<input type="hidden" name="action_name" id="action_name" value="" />
	
	<input type="button" name="action" value="[[Delete]]" class="deletebutton" onclick="if ( confirm('[[Are you sure you want to delete selected ZipCode(s)?]]') ) submitForm('delete');">
	<div class="clr"><br/></div>
	
	<table>
		<thead>
			<tr>
				<th><input type="checkbox" id="all_checkboxes_control"></th>
				<th><a href="?sorting_field=name&sorting_order={if $sorting_order == 'ASC' && $sorting_field == 'name'}DESC{else}ASC{/if}&zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">[[Zip Code]]</a>
				{if $sorting_field == 'name'}{if $sorting_order == 'DESC'}<img src="{image}b_down_arrow.gif">{else}<img src="{image}b_up_arrow.gif">{/if}{/if}
				</th>
				<th><a href="?sorting_field=longitude&sorting_order={if $sorting_order == 'ASC' && $sorting_field == 'longitude'}DESC{else}ASC{/if}&zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">[[Longitude]]</a>
				{if $sorting_field == 'longitude'}{if $sorting_order == 'DESC'}<img src="{image}b_down_arrow.gif">{else}<img src="{image}b_up_arrow.gif">{/if}{/if}
				</th>
				<th><a href="?sorting_field=latitude&sorting_order={if $sorting_order == 'ASC' && $sorting_field == 'latitude'}DESC{else}ASC{/if}&zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">[[Latitude]]</a>
				{if $sorting_field == 'latitude'}{if $sorting_order == 'DESC'}<img src="{image}b_down_arrow.gif">{else}<img src="{image}b_up_arrow.gif">{/if}{/if}
				</th>
				<th><a href="?sorting_field=city&sorting_order={if $sorting_order == 'ASC' && $sorting_field == 'city'}DESC{else}ASC{/if}&zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">[[City]]</a>
				{if $sorting_field == 'city'}{if $sorting_order == 'DESC'}<img src="{image}b_down_arrow.gif">{else}<img src="{image}b_up_arrow.gif">{/if}{/if}
				</th>
				<th><a href="?sorting_field=state&sorting_order={if $sorting_order == 'ASC' && $sorting_field == 'state'}DESC{else}ASC{/if}&zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">[[State]]</a>
				{if $sorting_field == 'state'}{if $sorting_order == 'DESC'}<img src="{image}b_down_arrow.gif">{else}<img src="{image}b_up_arrow.gif">{/if}{/if}
				</th>
				<th><a href="?sorting_field=state_code&sorting_order={if $sorting_order == 'ASC' && $sorting_field == 'state_code'}DESC{else}ASC{/if}&zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">[[State Code]]</a>
				{if $sorting_field == 'state_code'}{if $sorting_order == 'DESC'}<img src="{image}b_down_arrow.gif">{else}<img src="{image}b_up_arrow.gif">{/if}{/if}
				</th>
				<th><a href="?sorting_field=country_name&sorting_order={if $sorting_order == 'ASC' && $sorting_field == 'country_name'}DESC{else}ASC{/if}&zip_codes_per_page={$zip_codes_per_page}&{$searchQuery}">[[Country]]</a>
				{if $sorting_field == 'country_name'}{if $sorting_order == 'DESC'}<img src="{image}b_down_arrow.gif">{else}<img src="{image}b_up_arrow.gif">{/if}{/if}
				</th>
				<th colspan=2 class="actions">[[Actions]]</th>
			</tr>
		</thead>
		<tbody>
			{foreach from=$location_collection item=location name=location_block}
			<tr class="{cycle values='oddrow,evenrow'}">
				<td><input type="checkbox" name="locations[{$location.sid}]" value="1" id="checkbox_{$smarty.foreach.location_block.iteration}" /></td>
				<td>{$location.name}</td>
				<td>{$location.longitude}</td>
				<td>{$location.latitude}</td>
				<td>{$location.city}</td>
				<td>{$location.state}</td>
				<td>{$location.state_code}</td>
				<td>{$location.country_name}</td>
				<td><a href="{$GLOBALS.site_url}/geographic-data/edit-location/?sid={$location.sid}" title="Edit" class="editbutton">[[Edit]]</a></td>
				<td><a href="?action=delete&location_sid={$location.sid}" onclick="return confirm('[[Are you sure you want to delete this data?]]')" title="[[Delete]]" class="deletebutton">[[Delete]]</a></td>
			</tr>
			{/foreach}
		</tbody>
	</table>
	
	<div class="clr"><br/></div>
	<input type="button" name="action" value="[[Delete]]" class="deletebutton" onclick="if (confirm('[[Are you sure you want to delete selected ZipCode(s)?]]')) submitForm('delete');">
</form>
<script>
	var total={$smarty.foreach.location_block.total};
	{literal}

	function set_checkbox(param) {
		for (i = 1; i <= total; i++) {
			if (checkbox = document.getElementById('checkbox_' + i)) {
				checkbox.checked = param;
			}
		}
	}

	$("#all_checkboxes_control").click(function () {
		if (this.checked == false)
			set_checkbox(false);
		else
			set_checkbox(true);
	});

	function submitForm(action) {
		document.getElementById('action_name').value = action;
		var form = document.locations_form;
		form.submit();
	}

	$(".setting_button").click(function () {
		var butt = $(this);
		$(this).next(".setting_block").slideToggle("normal", function(){
				if ($(this).css("display") == "block") {
					butt.children(".setting_icon").html("<div id='accordeonOpen'></div>");
					butt.children("strong").text("Click to hide search criteria");
				} else {
					butt.children(".setting_icon").html("<div id='accordeonClosed'></div>");
					butt.children("strong").text("Click to modify search criteria");
				}
			});
	});

	function getStates(countrySID) {
		if ($("#country").val() == '') {
			$("#state").attr("disabled", "disabled");
		} else {
			$("#state").removeAttr("disabled");
		}
		$.get("{/literal}{$GLOBALS.site_url}{literal}/get-states/", { country_sid: countrySID, state_sid: "", parentID: "Location", display_as: "", type: "zipCodeSearch" },
			function (data) {
				var stateField = $("#state");
				if (data != '') {
					stateField.html(data);
					stateField.find("option[value='" + stateField.attr('data-old-value') + "']").attr("selected", "selected");
				}
			});
	}

	$(document).ready(function() {
		getStates($("#country").val());
	});

	{/literal}
	{if $search}
		{literal}
		var butt = $(".setting_button");
		butt.next(".setting_block").slideToggle("normal", function () {
			butt.children(".setting_icon").html("<div id='accordeonOpen'></div>");
			butt.children("strong").text("Click to hide search criteria");
		});
		{/literal}
	{/if}
</script>
